/*
 * DevicesTimer.c
 *
 *      Author: Honrun
 */
#include <stdio.h>
#include <stdint.h>
#include "driver/gptimer.h"
#include "DevicesTimer.h"


static bool example_timer_on_alarm_cb_v159(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_data)
{
    static int st_iCnt = 0;

    gptimer_stop(timer);

    printf("example_timer_on_alarm_cb_v159: %d\r\n", st_iCnt++);

    return 0;
}

int8_t cTimer0Init(void)
{
    gptimer_config_t timer_config;
    gptimer_handle_t timer_handle;
    gptimer_event_callbacks_t cbs;
    gptimer_alarm_config_t alarm_config;

    timer_config.clk_src = GPTIMER_CLK_SRC_APB;
    timer_config.direction = GPTIMER_COUNT_UP;
    timer_config.resolution_hz = 1000000;
    timer_config.intr_priority = 0;
    timer_config.flags.intr_shared = 0;
    timer_config.flags.backup_before_sleep = 0;
    gptimer_new_timer(&timer_config, &timer_handle);

    cbs.on_alarm = example_timer_on_alarm_cb_v159;
    gptimer_register_event_callbacks(timer_handle, &cbs, NULL);

    gptimer_set_raw_count(timer_handle, 0);

    gptimer_enable(timer_handle);

    alarm_config.alarm_count = 500000;
    alarm_config.reload_count = 500000;
    alarm_config.flags.auto_reload_on_alarm = 1;
    gptimer_set_alarm_action(timer_handle, &alarm_config);

    gptimer_start(timer_handle);

    return 0;
}
